<?php

class Core_Validate_Abstract extends Zend_Validate_Abstract
{
	public function isValid($value = true)
	{
		return empty($this->_errors);
	}

	public function validate($valid = true)
	{
		$rules = $this->_rules;
		$value = $this->_value;
		$validrules = ($valid===true)?array_keys($rules):explode(',', $valid);
		
		foreach($validrules as $rule)
		if(!empty($rules[$rule]['valid']))
		{
			$validators = new Zend_Validate();
			foreach($rules[$rule]['valid'] as $validator)
				$validators->addValidator($validator);
			
			if(!$validators->isValid($value->{$rule}))
			{
				$this->_messages[$rule] = $validators->getMessages();
				$this->_errors[] = $rule;
			}
		}
	}
	
	public function filter($index)
  {
		$rules 	= $this->_rules;
		$value 	= $this->_value->{$index};
		
		if(!empty($rules[$index]['filter']))
		{
			foreach($rules[$index]['filter'] as $filter)
			{
				$value = $filter->filter($value);
			}
		}
    return $value;
  }
}